package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.LookReqPara;
import com.lst.entity.response.WenResPara;
import com.lst.model.ClientNum;
import com.lst.model.WenwInfo;
import com.lst.service.ClientNumService;
import com.lst.service.MstDiscussService;
import com.lst.service.WenwInfoService;
import com.lst.utils.CommFunUtils;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: WenDetailServlet
 * @Description: 文章详情接口
 * @author zmm
 * @date 2017年10月10日 
 *
 */
public class WenDetailServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID 
	 */
	private static final long serialVersionUID = 1L;

	private WenwInfoService wenwInfoService = ctx.getBean(WenwInfoService.class);

	private ClientNumService clientNumService = ctx.getBean(ClientNumService.class);
	
	private MstDiscussService mstDiscussService = ctx.getBean(MstDiscussService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();

		WenResPara resPara = new  WenResPara();

		try {
			LookReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer clientid = Integer.valueOf(reqPara.getUserid());
				Integer wenId = Integer.valueOf(reqPara.getWenId());
				WenwInfo wen=wenwInfoService.selectByPrimaryKey(wenId);
				if(wen!=null){
					//收藏量
					Integer favoritenum = CommFunUtils.opNum(null, wen.getId(), CommCode.SYSTEM_CARESEL_RANGE_WENW, CommCode.SYSTEM_COUNT_FAVORITE, clientNumService);
					//分享量
					Integer sharenum = CommFunUtils.opNum(null, wen.getId(), CommCode.SYSTEM_CARESEL_RANGE_WENW, CommCode.SYSTEM_COUNT_SHARE, clientNumService);
					//浏览量
					Map<String, Object> lookMap = new HashMap<String, Object>();
					lookMap.clear();
					lookMap.put("enabled", true);
					lookMap.put("isDeleted", false);	
					lookMap.put("numtype", CommCode.SYSTEM_COUNT_BROWSE);
					lookMap.put("actid", wen.getId());
					lookMap.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_WENW);

					List<ClientNum> browselist = clientNumService.queryByList(lookMap);

					Integer browsenum = 0;

					for(ClientNum c : browselist){
						browsenum += c.getNum() == null ? 0 : c.getNum();
					}

					wen.setFavoritenum(favoritenum);
					wen.setSharenum(sharenum);
					wen.setBrowsenum(browsenum);
					
					//评论量
					Map<String, Object> wenmap = new HashMap<String, Object>();

					wenmap.put("enabled", true);
					wenmap.put("iseleted", false);
					wenmap.put("tarid", wen.getId());
					wenmap.put("type", CommCode.SYSTEM_CARESEL_RANGE_WENW);
					Integer wendiscussnum = mstDiscussService.queryCountByMap(wenmap);

					wen.setReserved2(wendiscussnum);
					
					List<ClientNum> cns = null;
					if(!StringUtils.isEmpty(clientid)){
						Map<String, Object> cnmap = new HashMap<String, Object>();

						cnmap.put("enabled", true);
						cnmap.put("isDeleted", false);
						cnmap.put("clientid", Integer.valueOf(clientid));
						cnmap.put("numtype",CommCode.SYSTEM_COUNT_FAVORITE);
						cnmap.put("actid", wen.getId());
						cnmap.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_WENW);

						cns = clientNumService.queryByList(cnmap);
					}
					if(null==cns||cns.size()==0){
						wen.setIsFavorite(false);
					}else{
						wen.setIsFavorite(true);
					}
				
	                resPara.setWenw(wen);
					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage(CommCode.M_Y000001);
				}else{
					resPara.setCode(CommCode.M_A000001);
                	resPara.setMessage(CommCode.M_A000002);
				}
				
				
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info(jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public LookReqPara getReqPara(HttpServletRequest request) {

		LookReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new LookReqPara());

			reqPara = (LookReqPara) baseRequest.clone();

			String wenId = request.getParameter("wenId");
			String userid = request.getParameter("userid");

			logger.info("reqPara wenId: " + wenId);
			logger.info("reqPara userid: " + userid);

			reqPara.setWenId(wenId);
			reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		LookReqPara lookReqPara = (LookReqPara) reqPara;
		WenResPara lookResPara = (WenResPara) resPara;

		if (StringUtils.isEmpty(lookReqPara.getWenId())) {
			lookResPara.setCode(CommCode.M_A000000);
			lookResPara.setMessage("文章id为空");
		} else if (StringUtils.isEmpty(lookReqPara.getUserid())) {
			lookResPara.setCode(CommCode.M_A000000);
			lookResPara.setMessage(CommCode.M_P000002);
		}else{
			flag = true;
		}

		return  flag;
	}
}
